var vueApp = new Vue({
  el: '#app',
  data: function () {
    return {
    	  tableId:'1',
    	  tableName:'',
    	  tempUrl:'',
    	  impDate:[],
    	  impSts:'-1',
    	  impOptions:[],    	  
    	  impData:[],
    	  currentPageNbr: config.currentPageNbr,
          pageSizes: config.pageSizes,
          pageSize: config.defaultpageSize,
          totalRecords: 0,
          uploadUrl:'../../pub/import/upload',
          data:{},
          typeLimit:['application/vnd.ms-excel','application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'],
      	  fileFlag: false,      	  
          fileUploadPercent:0,
          detailurl:'importdetail.html'
      };
  },
  mounted: function () {
    this.initData();

  },
  methods: {
    initData:function(){
       var that = this;
       this.tableId = tools.getQueryString("tableId")==""?this.tableId:tools.getQueryString("tableId");
       this.tableInfo();
       this.impOptions.push({"code":"-1","code_desc":"全部","display_order":"0"});
       import_status.forEach(function(item){
    	   if(item.code!="import_status_5"){
    		   that.impOptions.push(item);
    	   }
       });
       this.onSearch();
       
    },
    template:function(){
    	var filename = this.tableName+"."+this.tempUrl.split("\.")[1];
		var filepath = this.tempUrl;    
		var form="<form id='download' action='../../pub/attach/download' method='post'>";
		form +="<input type='hidden' name='filename'  value='"+filename+"' />";
		form +="<input type='hidden' name='filepath'  value='"+filepath+"' />";
		form +="</form>";
		$("#header").append(form);
		$("#download").submit();
		$("#download").remove();
    },
    tableInfo:function(){
    	var that = this;
    	var reqData={
    	   "tableId": this.tableId  	
        };
        tools.ajaxRequestAsyncEx("pub/import/tableinfo", reqData, function(data){
    	   	if (data.respCode != 0) {
    	   		that.showError(data.respDesc);    
    	   		return false;
    	   	}else{
    	   		var info = data.list[0];
    	   		that.tableName = info.table_name;
    	   		that.tempUrl = info.template_url;
    	   	}    	   			
    	 });    	
    },
    load:function(){
    	this.uploadUrl = "../../pub/import/upload";
    	$(".avatar-uploader .el-upload__input").click();
    },    
    beforeUploadFile: function(file) {
    	this.data.fileName= file.name;
    	this.data.fileType = file.type;
    	this.data.fileSize = file.size;       
		this.data.modeName = "数据录入";
		this.data.folder= "/file/imp";
        this.data.modeName="上传";
        this.data.tableId = this.tableId;
    	
        const isLt10M = file.size / 1024 / 1024  < 50;
        if (this.typeLimit.indexOf(file.type) == -1) {
        	this.showError("上传文件格式不正确");
            return false;
        }
        if (!isLt10M) {
        	this.showError("上传文件大小不能超过50MB!");
            return false;
        }
    },
    uploadFileProcess: function(event, file, fileList){
	  var that = this;          
	  var complete = (event.loaded / event.total * 100 | 0);
      that.$showProgress(complete);
    },
    handleFileSuccess: function (res, file) {   
	  var that =this;
      this.search();
      if(res.respCode=="0"){
    	  setTimeout(function(){
        	  that.$showProgress(false);
        	  that.$message({
     	         showClose: true,
     	         type: 'success',
     	         message: res.respDesc
     	      });
          },2000);
      }else if(res.respCode!="-1"){
    	  setTimeout(function(){
        	  that.$showProgress(false);
        	  that.$message.error(res.respDesc);
          },2000);
      }else{
    	  setTimeout(function(){
        	  that.$showProgress(false);
        	  that.showError(res.respDesc);
          },2000);  
      }
  	},
  	handleFileError:function(err, file, fileList){
	  this.$showProgress(false);
  	},
    shorten:function(txt){
    	var result=txt;
    	if(result.length>20){
    		return result.substr(0,20)+"...";
    	}
    	return result;
    },
    download:function(row){
    	var filename = row.file_name;
		var filepath = row.file_url;    
		var form="<form id='download' action='../../pub/attach/download' method='post'>";
		form +="<input type='hidden' name='filename'  value='"+filename+"' />";
		form +="<input type='hidden' name='filepath'  value='"+filepath+"' />";
		form +="</form>";
		$("#impTable").append(form);
		$("#download").submit();
		$("#download").remove();
    },
    view:function(row){
    	var url = this.detailurl+"?importId="+row.import_id+"&impStatus="+row.import_status+"&tableId="+this.tableId;
    	window.open(url);
    },
    transfer:function(row){
    	var that =this;
    	this.$confirm('是否确认迁移文件 【'+row.file_name+'】数据 ?', '提示', {
             confirmButtonText: '确定',
             cancelButtonText: '取消',
             type: 'warning'
        }).then(() => {
        	var jso = {
            	"import_id": row.import_id,
            	"tableName": that.tableName,
            	"tableId": that.tableId
            };
        	var reqData={
        		"jsonStr":JSON.stringify(jso) 			
        	};
        	tools.ajaxRequestAsyncEx("pub/import/subImportData", reqData, function(data){
        		if (data.respCode != 0) {
        			that.showError(data.respDesc);    
        			return false;
        		}else{
		   			that.$message({
		                showClose: true,
		                type: 'success',
		                message: '迁移成功!'
		            });  
		   			that.search();
        		}    	   			
        	});	
        	
        }).catch(() => {       
        	
        });
    },
    check:function(row){
    		var that = this;
    		var jso = {
        		"import_id": row.import_id,
        		"tableId": this.tableId
        	};
        	var reqData={
        		"jsonStr":JSON.stringify(jso) 			
    	    };
        	tools.ajaxRequestAsyncEx("pub/import/check", reqData, function(data){
    	   		if (data.respCode != 0) {
    	   			that.showError(data.respDesc);    
    	   			return false;
    	   		}else{
    	   			that.$message({
    	                showClose: true,
    	                type: 'success',
    	                message: data.respDesc
    	            });  
    	   		}    	   			
    	    });   
        	
    },
    reload:function(row){
    	this.uploadUrl = "../../pub/import/reload";
        this.data.importId = row.import_id;
     	this.data.fileurl = row.file_url;
    	$(".avatar-uploader .el-upload__input").click();
    },
    del:function(row){
    	var that =this;
    	that.$confirm('是否确认删除文件 '+row.file_name+' ?', '提示', {
             confirmButtonText: '确定',
             cancelButtonText: '取消',
             type: 'warning'
        }).then(() => {
        	var jso = {
        		"import_id": row.import_id,
        		"file_url": row.file_url
        	};
        	var reqData={
        		"jsonStr":JSON.stringify(jso) 			
    	    };
        	tools.ajaxRequestAsyncEx("pub/import/del", reqData, function(data){
    	   		if (data.respCode != 0) {
    	   			that.showError(data.respDesc);    
    	   			return false;
    	   		}else{
    	   			that.$message({
    	                showClose: true,
    	                type: 'success',
    	                message: '删除成功!'
    	            });  
    	   			that.search();
    	   		}    	   			
    	    });
        	
        }).catch(() => {
           		return false;
        });
    },
   
    onSearch:function(){
      var that = this;
      console.log(this.impDate);
      var beginDate = (this.impDate!=null&&this.impDate.length>0)?util.formatTime(this.impDate[0],'yyyy-MM-dd'):"";
      var endDate = (this.impDate!=null&&this.impDate.length>0)?util.formatTime(this.impDate[1],'yyyy-MM-dd'):"";
      var jso = {
    	"startDate": beginDate,
    	"endDate": endDate,
    	"tableId": this.tableId,
    	"status": this.impSts
      };
      var postdata = {
    	  "jsonStr":JSON.stringify(jso),    		 
          "currentPageNbr" : this.currentPageNbr,
          "pageSize" : this.pageSize
      };
      
      tools.ajaxRequestAsyncEx("pub/import/list",postdata, function(data){
          if (data.respCode == "0") {
        	  console.log(data);
              that.impData = data.list;
              that.totalRecords = data.page.totalRecords;       
          } else {
              that.impData = [];
              that.totalRecords =0;
              that.currentPageNbr=1;
              that.showError(data.respDesc);      
          }
      });


    },
    
    search:function(){
    	this.currentPageNbr = 1;
        this.onSearch();
    },
    handleSizeChange:function(pageSize){
        this.pageSize = pageSize;
        this.currentPageNbr = 1;
        this.onSearch();
    },

    handleCurrentChange:function(currentPageNbr){
      this.currentPageNbr = currentPageNbr;
      this.onSearch();
    },

    showError: function(info){
        var i = (info==undefined?"加载数据错误":info);
        this.$alert(i, '系统错误', {
             confirmButtonText: '确定'
        }); 
    },

  },
  watch:{
	  
  }
})